package cn.com.greatwall.system.modules.oauth2.oauth;

import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler;

import cn.com.greatwall.system.modules.oauth2.service.OauthClientDetailsService;
import cn.com.greatwall.system.modules.oauth2.service.dto.OauthClientDetailsDto;

/**
 * @Author herw
 * @Time 2020-10-24 17:16:48
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
public class OauthUserApprovalHandler extends TokenStoreUserApprovalHandler {
    private OauthClientDetailsService oauthClientDetailsService;
    
    public OauthUserApprovalHandler() {
        
    }
    
    public void setOauthService(OauthClientDetailsService oauthClientDetailsService) {
        this.oauthClientDetailsService = oauthClientDetailsService;
    }
    
    public boolean isApproved(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
        if (super.isApproved(authorizationRequest, userAuthentication)) {
            return true;
        }
        if (!userAuthentication.isAuthenticated()) {
            return false;
        }

        OauthClientDetailsDto clientDetailsDto = oauthClientDetailsService.findById(authorizationRequest.getClientId());
        return clientDetailsDto != null;

    }
}
